{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "from collections import deque\n",
    "\n",
    "# 读取 HTML 文件\n",
    "with open(\"./demo.html\", \"r\") as f:\n",
    "    html = f.read()\n",
    "\n",
    "# 解析 HTML\n",
    "soup = BeautifulSoup(html, 'html.parser')\n",
    "\n",
    "id = 0\n",
    "queue = deque([soup.html])\n",
    "while queue:\n",
    "    id += 1\n",
    "    node = queue.popleft()\n",
    "    node['ann_id'] = id\n",
    "    for child in node.children:\n",
    "        if child.name is not None:  # 只将标签节点加入队列\n",
    "            queue.append(child)\n",
    "with open(\"./demo_with_id.html\",\"w\") as f:\n",
    "    f.write(soup.prettify())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "html:1\n",
      "head:2\n",
      "html:1 head:2\n",
      "body:3\n",
      "html:1 body:3\n",
      "head:2\n",
      "meta:4\n",
      "head:2 meta:4\n",
      "meta:5\n",
      "head:2 meta:5\n",
      "title:6\n",
      "head:2 title:6\n",
      "body:3\n",
      "p:7\n",
      "body:3 p:7\n",
      "p:8\n",
      "body:3 p:8\n",
      "p:9\n",
      "body:3 p:9\n",
      "meta:4\n",
      "meta:5\n",
      "title:6\n",
      "p:7\n",
      "p:8\n",
      "p:9\n"
     ]
    }
   ],
   "source": [
    "with open(\"./demo_with_id.html\", \"r\") as f:\n",
    "    html = f.read()\n",
    "soup = BeautifulSoup(html, 'html.parser')\n",
    "queue = deque([soup.html])\n",
    "while queue:\n",
    "    node = queue.popleft()\n",
    "    print(str(node.name) + \":\" + str(node['ann_id']))\n",
    "    # dot.node(str(node.name) + \":\" + str(node['ann_id']))\n",
    "    for child in node.children:\n",
    "        if child.name is not None:  # 只将标签节点加入队列\n",
    "            queue.append(child)\n",
    "            print(str(child.name) + \":\" + str(child['ann_id']))\n",
    "            # dot.node(str(child.name) + \":\" + str(child['ann_id']))\n",
    "            print(str(node.name) + \":\" + str(node['ann_id']), str(child.name) + \":\" + str(child['ann_id']))\n",
    "            # dot.edge(str(node.name) + \":\" + str(node['ann_id']), str(child.name) + \":\" + str(child['ann_id']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'demo_with_id.gv.pdf'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from graphviz import Digraph\n",
    "\n",
    "dot = Digraph()\n",
    "\n",
    "# Define nodes with simple names\n",
    "dot.node(\"html-1\", label=\"html:1\")\n",
    "dot.node(\"head-2\", label=\"head:2\")\n",
    "dot.node(\"body-3\", label=\"body:3\")\n",
    "dot.node(\"p-1\")\n",
    "dot.node(\"p-2\")\n",
    "\n",
    "# Define edges\n",
    "dot.edge(\"html\", \"head\")\n",
    "dot.edge(\"html\", \"body\")\n",
    "dot.edge(\"body\", \"p-1\")\n",
    "dot.edge(\"body\", \"p-2\")\n",
    "\n",
    "# Render the graph\n",
    "dot.render(\"demo_with_id.gv\", view=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
